// player class


var BULLETS_LIMIT = 10; // limit of bullets
var SHOOT_DELAY = 500; // 2 shoots each second

function Player(map, left, top) {
	Character.call(this, map, {
		left: left,
		top: top,
		width: 38,
		height: 36,
		jumpPower: 10,
		canEat: true,
		canDestroyObstacles: true,
		canDestroyRock: true,
		canShoot: true,
		speed: _speed,
		spriteData: {
			image: _images.object,
			frameWidth: 57,
			frameHeight: 57,
			interval: 300
		}
	});

	this.isFalling = false;
	this.isChild = true;
	this.immuneCount = 0;
	this.canShoot = false;
	this.canDestroyRock = false;
	this.bullets = [];
	this.isFaceRight = true;

	this.lastShoot = 0;
	this.live = NUM_LIFE;

	// add sprite
	this.addSprite({
		name: "jump_left",
		startFrame: 41,
		framesCount: 5,
		marginTop: 7,
		marginBottom: 7,
		marginLeft: 4,
		marginRight: 2
	});
	this.addSprite({
		name: "jump_right",
		startFrame: 36,
		framesCount: 5,
		marginTop: 7,
		marginBottom: 7,
		marginLeft: 4,
		marginRight: 2
	});
	this.addSprite({
		name: "stand_left",
		startFrame: 22,
		framesCount: 2,
		marginTop: 6,
		marginBottom: 6,
		marginLeft: 5,
		marginRight: 5
	});
	this.addSprite({
		name: "stand_right",
		startFrame: 20,
		framesCount: 2,
		marginTop: 6,
		marginBottom: 6,
		marginLeft: 5,
		marginRight: 5
	});
	this.addSprite({
		name: "run_left",
		startFrame: 30,
		framesCount: 6,
		marginTop: 5,
		marginBottom: 7,
		marginLeft: 5,
		marginRight: 2
	});
	this.addSprite({
		name: "run_right",
		startFrame: 24,
		framesCount: 6,
		marginTop: 5,
		marginBottom: 7,
		marginLeft: 5,
		marginRight: 2
	});

	this.addSprite({
		name: "adult_stand_right",
		startFrame: 48,
		framesCount: 2,
		marginTop: 0,
		marginBottom: 2,
		marginLeft: 7,
		marginRight: 9
	});
	this.addSprite({
		name: "adult_stand_left",
		startFrame: 50,
		framesCount: 2,
		marginTop: 0,
		marginBottom: 2,
		marginLeft: 7,
		marginRight: 9
	});
	this.addSprite({
		name: "adult_run_left",
		startFrame: 63,
		framesCount: 3,
		marginTop: 2,
		marginBottom: 3,
		marginLeft: 8,
		marginRight: 8
	});
	this.addSprite({
		name: "adult_run_right",
		startFrame: 60,
		framesCount: 3,
		marginTop: 2,
		marginBottom: 3,
		marginLeft: 8,
		marginRight: 8
	});
	this.addSprite({
		name: "adult_jump_left",
		startFrame: 54,
		framesCount: 2,
		marginTop: 0,
		marginBottom: 2,
		marginLeft: 8,
		marginRight: 7
	});
	this.addSprite({
		name: "adult_jump_right",
		startFrame: 52,
		framesCount: 2,
		marginTop: 0,
		marginBottom: 2,
		marginLeft: 8,
		marginRight: 7
	});
    
    this.addSprite({
		name: "die",
		startFrame: 0,
		framesCount: 5,
		marginTop: 5,
		marginBottom: 7,
		marginLeft: 2,
		marginRight: 2
	});
    
	// set the first sprite
	this.setSprite("stand_right");
}

Player.prototype = new Character();

/*
 shoot function
 */
Player.prototype.shoot = function() {

	if (this.canShoot && this.bullets.length < BULLETS_LIMIT) {
		var time = (new Date()).getTime();
		var d = time - this.lastShoot;

		if (d > SHOOT_DELAY) {
			this.lastShoot = time;

			var left = this.isFaceRight ? this.right : this.left - 10;
			var b = new Bullet(this.map, left - 3, this.top + this.height / 3); // posiion of bullet

			if (this.isFaceRight) {
				b.speedX = b.speed;
				b.isRight = true;
			} else {
				b.speedX = -b.speed;
				b.isRight = false;
			}

			this.bullets.push(b);
		}
	}
}

// player die
Player.prototype.die = function() {
    
    this.setSprite("die");
    if(!_isMute) {
        soundManager.stop("music");
        soundManager.play("game_over");
    }
    _live--;
	this.isFalling = true;
	this.speedY = 0;
}

// player grow
Player.prototype.grow = function() {

	if (this.isChild) // grow up
	{
		// width and height of black bird
		this.width = 34;
		this.height = 44;

		if (this.isFaceRight)
			this.setSprite("adult_stand_right");
		else
			this.setSprite("adult_stand_left");
	} else { // grow down
		// width and height of red bird
		this.width = 38;
		this.height = 36;

		if (this.isFaceRight)
			this.setSprite("stand_right");
		else
			this.setSprite("stand_left");

		this.immuneCount = FPS; // make character immune for 1 seconds
		this.canShoot = false;
	}
	this.top = this.bottom - this.height;
	this.isChild = !this.isChild;
}

Player.prototype.draw = function(context) {

	AnimatedSprite.prototype.draw.call(this, context, this.map.offsetX, this.map.offsetY);

	for (var i = 0; i < this.bullets.length; i++)
		this.bullets[i].draw(context);
}

Player.prototype.update = function() {

	// check when player collision with end point of game
	var p = this.map.endPoint;

	if (this.left <= p.x && this.top <= p.y && this.right >= p.x && this.bottom >= p.y) {
		// win
        console.log("End point");
        if(this.map.monsters[0] == undefined) {
            return 2;
        } else {
            if(!this.map.monsters[0].isKing) {
                return 2;
            }
        }
	}

	if (this.immuneCount > 0)
		this.immuneCount--;

	if (this.isFalling) // die
	{
		// set sprite die
		this.setSprite("die");

		// falling
		for (var i = 0; i < 10; i += 1) {
			this.top -= 1;
		}

		this.speedY += GRAVITY;
		this.top += this.speedY;

		return (this.top > this.map.height) ? 1 : 0;
	}

	var b = Character.prototype.update.call(this);

	if (b == true) // ate the flower
	{
		if (this.isChild)
			this.grow();

		this.canShoot = true;
	}

	this.collide();

	// update bullets
	if (this.bullets.length > 0) {
		var i = 0;
		var length = this.bullets.length;

		while (i < length) {

			// destroy bullets
			if (this.bullets[i].isDead || this.bullets[i].left < this.offsetX) {
				this.bullets.splice(i, 1);
				length--;
			} else {
				this.bullets[i].update();
				i++;
			}
		}
	}

	// pause the big pig when the player on the head of them
	for (m in this.map.monsters) {
		var mon = this.map.monsters[m];
		if (mon.pause) {
			var time = (new Date()).getTime();
			var d = time - mon.lastPause;

			if (d > 5000) {
				mon.lastPause = time;

				mon.canDamage = true;
				mon.pause = false;
				mon.speedX = mon.speedY = 2;
			}
		}
	}
}

Player.prototype.handleInputs = function(keyStates) {

	this.speedX = 0;
	var isRunning = true;

	if (keyStates[Keys.DOWN_ARROW])
		this.speedY = this.speed;

	if (keyStates[Keys.LEFT_ARROW]) {
		this.speedX = -this.speed;
		this.isFaceRight = false;
	} else if (keyStates[Keys.RIGHT_ARROW]) {
		this.speedX = this.speed;
		this.isFaceRight = true;
	} else {
		isRunning = false;
	}

	if (keyStates[Keys.UP_ARROW] && !this.isJumping) {
        soundManager.play("jump");
		this.jump();
	}

	if (keyStates[Keys.SPACE]) // shoot
	{
        if(!_isMute) {
            if(this.canShoot)
                soundManager.play("slingshot");
        }
		this.shoot();
	}

	// set sprite
	if (this.isChild) {
		if (this.isJumping)
			this.setSprite(this.isFaceRight ? "jump_right" : "jump_left");
		else if (isRunning)
			this.setSprite(this.isFaceRight ? "run_right" : "run_left");
		else
			this.setSprite(this.isFaceRight ? "stand_right" : "stand_left");
	} else {
		if (this.isJumping)
			this.setSprite(this.isFaceRight ? "adult_jump_right" : "adult_jump_left");
		else if (isRunning)
			this.setSprite(this.isFaceRight ? "adult_run_right" : "adult_run_left");
		else
			this.setSprite(this.isFaceRight ? "adult_stand_right" : "adult_stand_left");
	}

}

Player.prototype.collide = function() {

	// collide with monsters
	for (m in this.map.monsters) {

		var mon = this.map.monsters[m];
		if (!mon)
			continue;
        if(this.collideObj(this, mon)) {
			// when player jump monster's head
			if (this.bottom > mon.top && this.speedY > 0) {
				// effect when monsters die
				if (mon.typeMonster == "small") {
				    //alert("va cham");
				    if(!_isMute) {
                        soundManager.play("click");
                    }
					mon.setSprite("die");
					mon.die();
					//mon.setSprite("die");
				} else if (mon.typeMonster == "big" && !mon.pause) {
                    
					mon.speedX = mon.speedY = 0;
					mon.pause = true;
				} else if (mon.typeMonster == "big" && mon.pause) {
				    if(!_isMute) {
				        soundManager.play("click");
				    }
				}
			} else {
				if (this.isChild) {
					if (this.immuneCount == 0) {
						if (!mon.pause) {
						  
                            var mush = new Mushroom(this.map, 100, 200);
                            if(_speed == MAX_SPEED) {                            
					           _speed = MIN_SPEED;
                               return;
                            } else {
                               this.die(); 
                            }
							break;
						}
					}
				} else if (!mon.pause){
				    _speed = MIN_SPEED;
				    this.grow(); // grow down
				}	
			}
		}
	}

	// 
	for (m in this.map.plants) {
		var mon = this.map.plants[m];

		if (!mon || !mon.canDamage)
			continue;

		if (!(this.left > mon.right ||
			this.right < mon.left ||
			this.top > mon.bottom ||
			this.bottom < mon.top)) {
			if (this.isChild) {
				if (this.immuneCount == 0) {
					this.die();
					break;
				}
			} else
				this.grow(); // grow down				
		}
	}

	// other mushroom
	for (var i = 0; i < this.map.bullets.length; i++) {

		var m = this.map.bullets[i];
        
        if(this.collide(this, m)) {
            alert("aan");
        }
		if (!(this.left > m.right ||
			this.right < m.left ||
			this.top > m.bottom ||
			this.bottom < m.top)) {
			 
            if(!_isMute) {
                soundManager.play("eat_fruit");
            }
			if (this.isChild)
				this.grow();
			else
				this.map.scores += 100;
			m.die();
		}
	}
}